<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'

const ipInputRef = ref(null)
const operList = [
    {
        title: '状态',
        childen: [
            {
                label: '开始游戏',
                dataType: 1,
                cmd: 0,
                value: 'StartGame'
            },
            {
                label: '重新开始',
                dataType: 5,
                cmd: 0,
            },
            {
                label: '退出游戏',
                dataType: 5,
                cmd: 1,
            },
        ]
    },
    {
        title: '训练',
        childen: [
            {
                label: '跳过蓝牙',
                dataType: 2,
                cmd: 0,
            },
            {
                label: '开始训练',
                dataType: 2,
                cmd: 1,
            },
            {
                label: '训练结束',
                dataType: 2,
                cmd: 2,
            },
            {
                label: '重新训练',
                dataType: 2,
                cmd: 3,
            },
            {
                label: '上一步',
                dataType: 2,
                cmd: 4,
            },
            {
                label: '下一步',
                dataType: 2,
                cmd: 5,
            },
        ]
    },

]

let wsService: any = null
const formData = reactive({
    dataType: 3, //0 表示脑机指令暂时用不上，1表示作弊指令,3表示开关作弊（cmd = 0表开脑机，关作弊，cmd = 1表关脑机，开作弊。）
    message: {
        cmd: 0,
        data: { //根据cmd类型具体业务具体协商，也可以没有data
            value: ''
        }
    }
})

const tabIndex = ref(0)


function open(service: any) {
    console.log('open果冻人+++++++', service)
    wsService = service
}

function getMessage(e: any) {
    // 创建字节(byte)数组

    const buffer = Buffer.from(e.data); // 转换为缓冲区对象
    const string = JSON.parse(buffer.toString('utf8')); // 转换为字符串
    console.log('获取SockerMessage', string)
    // this.bluetoothList = string.message.data
}

function sendMessage(params: any) {

    formData.dataType = params.dataType
    formData.message.cmd = params.cmd
    formData.message.data.value = params.value || ''
    console.log(formData)
    wsService.send({ data: JSON.stringify(formData) })
}


onMounted(async () => {

});
</script>

<template>
    <div class="sea">
        <VCard v-for="(operItem, index) in operList" :title="operItem.title">
            <div>
                <el-button @click="sendMessage(item)" v-for="(item, index) in operItem.childen" type="primary"
                    class="title-botton">{{ item.label
                    }}</el-button>
            </div>
        </VCard>
        <IpInput ref="ipInputRef" @open="open" @getMessage="getMessage" ipKey="manIp" :formData="formData"></IpInput>
    </div>
</template>
<style lang="scss" scoped>
.botton-box {
    padding: 20px 0;
}

.title-box {
    padding-top: 20px;
}

.title-botton {
    min-width: 120px;
    height: 36px;
    line-height: 36px;
}
</style>